package main

import (
	"net/http"
	"io/ioutil"
	"fmt"
	"strings"
)

func localURL(link string) string {
	var p string
	prefixes := []string{ "http://", "https://", "ftp://" }
	for _, prefix := range prefixes {
		if strings.HasPrefix(link, prefix) {
			p = prefix
		}
	}
	if p != "" {
		return strings.Replace(
			fmt.Sprintf("http://127.0.0.1:8080/local/%s", 
					link[len(p):]),	"\\", "/", -1)
	}
	return link
}


func serveLocal(w http.ResponseWriter, r *http.Request) {
	filename := home + "/.recp/" + r.RequestURI[7:]
	if strings.HasSuffix(filename, "/") {
		filename += "index.html"
	}
	b, err := ioutil.ReadFile(filename)
	if err != nil {
		http.Error(w, "File not found", 404)
	}
	ctype := http.DetectContentType(b)
	if strings.HasSuffix(filename, "css") {
		ctype = "text/css"
	}
	w.Header().Set("Content-type", ctype)
	w.Write(b)
}
